﻿using Kendo.Mvc.UI;
using LineOfBusiness.Controls.Base;
using LineOfBusiness.Controls.Extensions;
using LineOfBusiness.Core.Helper;
using LineOfBusiness.Poco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LineOfBusiness.Controllers
{
    public class SharedController : LOBController
    {
        public ActionResult ModernModule()
        {
            ViewBag.FunctionTransactionList = CacheHelper.FunctionList.Values.Where(x => x.FunctionType != "M" && x.IsActive == true && x.ParentID.IndexOf("T") > 0).OrderBy(o => o.Sorting).ToList();
            ViewBag.FunctionSetupList = CacheHelper.FunctionList.Values.Where(x => x.FunctionType != "M" && x.IsActive == true && x.ParentID.IndexOf("S") > 0).OrderBy(o => o.Sorting).ToList();
            ViewBag.FunctionReportList = CacheHelper.FunctionList.Values.Where(x => x.FunctionType != "M" && x.IsActive == true && x.ParentID.IndexOf("P") > 0).OrderBy(o => o.Sorting).ToList();

            ViewBag.FunctionList = CacheHelper.FunctionList.Values.Where(x => x.FunctionType == "M" && x.IsActive == true).OrderBy(o => o.Sorting).ToList();
            return View("ModernModule");
        }

        public ActionResult LeftMenu()
        {
            ViewBag.FunctionTransactionList = CacheHelper.FunctionList.Values.Where(x => x.FunctionType != "M" && x.IsActive == true && x.ParentID.IndexOf("T") > 0).OrderBy(o => o.Sorting).ToList();
            ViewBag.FunctionSetupList = CacheHelper.FunctionList.Values.Where(x => x.FunctionType != "M" && x.IsActive == true && x.ParentID.IndexOf("S") > 0).OrderBy(o => o.Sorting).ToList();
            ViewBag.FunctionReportList = CacheHelper.FunctionList.Values.Where(x => x.FunctionType != "M" && x.IsActive == true && x.ParentID.IndexOf("P") > 0).OrderBy(o => o.Sorting).ToList();

            ViewBag.FunctionList = CacheHelper.FunctionList.Values.Where(x => x.FunctionType == "M" && x.IsActive == true).OrderBy(o => o.Sorting).ToList();
            return View("LeftMenu");
        }

        [HttpPost]
        public ActionResult MessageBox()
        {
            ViewBag.OK = 0;
            ViewBag.YES = 0;
            ViewBag.NO = 0;
            ViewBag.CANCEL = 0;
            ViewBag.MESSAGE = ViewData["message"];

            string messageCode = ViewData["messageCode"] as string;

            if (!string.IsNullOrEmpty(messageCode))
            {
                SYS_Messages Msg = CacheHelper.GetMessagesCache(messageCode); 

                if (Msg != null)
                {
                    ViewBag.MESSAGE = Msg.CustomName;
                }
            }

            var ebutton = (ViewData["button"] as string).ToUpper();
            if (ebutton == "OK".ToUpper())
            {
                ViewBag.OK = 1;
            }

            if (ebutton == "OKCancel".ToUpper())
            {
                ViewBag.OK = 1;
                ViewBag.CANCEL = 1;
            }

            if (ebutton == "YesNo".ToUpper())
            {
                ViewBag.YES = 1;
                ViewBag.NO = 1;
            }

            if (ebutton == "YesNoCancel".ToUpper())
            {
                ViewBag.YES = 1;
                ViewBag.NO = 1;
                ViewBag.CANCEL = 1;
            }

            if (Request.IsAjaxRequest())
            {
                string viewString = ViewRenderer.RenderView("~/Views/Shared/MessageBox.cshtml", null, this.ControllerContext);
                return Json(viewString);
            }

            return View();
        }

        public ActionResult MessageBox(string messageCode, string message, string Button, string id, string para1)
        {
            ViewBag.OK = 0;
            ViewBag.YES = 0;
            ViewBag.NO = 0;
            ViewBag.CANCEL = 0;
            ViewBag.MESSAGE = message;
            ViewBag.id = id;
            messageCode = ViewData["messageCode"] as string;

            if (!string.IsNullOrEmpty(messageCode))
            {
                SYS_Messages Msg = CacheHelper.GetMessagesCache(messageCode); 

                if (Msg != null)
                {
                    if (!string.IsNullOrEmpty(para1))
                        Msg.CustomName = string.Format(Msg.CustomName, para1);
                    ViewBag.MESSAGE = Msg.CustomName;
                }
            }

            if (string.IsNullOrEmpty(Button)) Button = "OK";
            Button = Button.ToUpper();
            if (Button == "OK")
            {
                ViewBag.OK = 1;
            }

            if (Button == "OKCancel".ToUpper())
            {
                ViewBag.OK = 1;
                ViewBag.CANCEL = 1;
            }

            if (Button == "YesNo".ToUpper())
            {
                ViewBag.YES = 1;
                ViewBag.NO = 1;
            }

            if (Button == "YesNoCancel".ToUpper())
            {
                ViewBag.YES = 1;
                ViewBag.NO = 1;
                ViewBag.CANCEL = 1;
            }

            return View();
        }


        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public JsonResult FunctionModule(string module, string id)
        {
            string parent = "";
            if (id != null)
            {
                parent = id;
            }
            else
            {
                parent = module;
            }
            var m = CacheHelper.FunctionList[module];
            var functions = CacheHelper.FunctionList.Values.Where(x => x.FunctionType != "M" && x.IsActive == true && x.Module == module && x.ParentID == parent).OrderBy(o => o.Sorting).ToList();

            var fs = from e in functions
                     select new
                     {
                         imageUrl = string.IsNullOrEmpty(e.SmallIcon) ? null : LineOfBusiness.Controls.Base.HtmlExtensions.GetBaseUrl + "Content/default/" + e.SmallIcon,
                         id = e.FunctionID,
                         Name = e.CustomName,
                         hasChildren = string.IsNullOrEmpty(parent) || CacheHelper.FunctionList.Values.Where(x => x.FunctionType != "M" && x.IsActive == true && x.Module == module && x.ParentID == e.FunctionID).Any(),
                         urlFunction = string.IsNullOrEmpty(e.Url) ? "" : LineOfBusiness.Controls.Base.HtmlExtensions.GetBaseUrl + e.Url,
                         displayMode = e.DisplayMode,
                         width = e.Width,
                         height = e.Height,
                         CustomName = e.CustomName
                     };

            return Json(fs, JsonRequestBehavior.AllowGet);
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public JsonResult Functions()
        {
            var functions = CacheHelper.FunctionList.Values.Where(x => x.IsActive == true && (x.IsFunction || x.FunctionType == "M")).OrderBy(o => o.CustomName).ToList();
            return Json(functions, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetFunction(string functionID)
        {
            var function = CacheHelper.FunctionList.Values.Where(x => x.IsActive == true && x.FunctionID == functionID).FirstOrDefault();

            return Json(function, JsonRequestBehavior.AllowGet);
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public JsonResult Modules()
        {
            var functions = CacheHelper.FunctionList.Values.Where(x => x.FunctionType == "M" && x.IsActive == true).OrderBy(o => o.Sorting).ToList();
            return Json(functions, JsonRequestBehavior.AllowGet);
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public JsonResult Transaction()
        {
            var functions = CacheHelper.FunctionList.Values.Where(x => x.FunctionType != "M" && x.IsActive == true && x.ParentID.IndexOf("T") > 0).OrderBy(o => o.Sorting).ToList();
            return Json(functions, JsonRequestBehavior.AllowGet);
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public JsonResult Report()
        {
            var functions = CacheHelper.FunctionList.Values.Where(x => x.FunctionType != "M" && x.IsActive == true && x.ParentID.IndexOf("P") > 0).OrderBy(o => o.Sorting).ToList();
            return Json(functions, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetFunctionAuto([DataSourceRequest]DataSourceRequest request)
        {
            string text = this.HttpContext.Request.QueryString["filter[filters][0][value]"].ToLower();

            var functions = CacheHelper.FunctionList.Values.Where(x => x.CustomName.ToLower().Contains(text) && x.FunctionType != "M").OrderBy(o => o.Sorting).ToList();

            var fs = from e in functions
                     select new
                     {
                         imageUrl = string.IsNullOrEmpty(e.SmallIcon) ? null : "/Content/default/" + e.SmallIcon,
                         id = e.FunctionID,
                         text = e.CustomName,
                         urlFunction = e.Url,
                         displayMode = e.DisplayMode,
                         width = e.Width,
                         height = e.Height,
                         CustomName = e.CustomName
                     };

            return Json(fs, JsonRequestBehavior.AllowGet);
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public ActionResult GetExecPer(string type, string functionID)
        {
            if (CacheHelper.LoginUser.Administrator)
                return Json(new { Active = true }, JsonRequestBehavior.AllowGet);
            else if (CacheHelper.LoginUser.CantChange && functionID == "ADSU04")
            {
                return Json(new { Active = false }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Active = CacheHelper.GetUserPermission(CacheHelper.LoginUser, functionID).Where(o => o.FType == type && o.FIsActive).Any() }, JsonRequestBehavior.AllowGet);
            }
                
        }

        public ActionResult ViewLanguage(string FormNameShow)
        {
            ViewBag.FormNameShow = FormNameShow;
            return View();
        }

        public ActionResult CaptureWebcam()
        {
            return View();
        }
        
	}
}